﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="Fahuo.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：Fahuo.cs
// 创建时间：2014-12-25 12:59
// 修改时间：2014-12-22 16:11
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

namespace Wxpay
{
    #region

    using System;
    using System.Collections.Generic;
    using System.Globalization;

    using Newtonsoft.Json;

    #endregion

    /// <summary>
    /// The fahuo.
    /// </summary>
    public class Fahuo
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Fahuo"/> class.
        /// </summary>
        /// <param name="out_trade_no">
        /// The out_trade_no.
        /// </param>
        /// <param name="transid">
        /// The transid.
        /// </param>
        public Fahuo(string out_trade_no, string transid)
        {
            this.Out_trade_no = out_trade_no;
            this.Transid = transid;

            // dom.LoadXml(xml);

            // var selectSingleNode = this.dom.SelectSingleNode("xml");
            // if (selectSingleNode != null)
            // {
            // this.OpenId = selectSingleNode.SelectSingleNode("OpenId").InnerText;

            // this.AppId = selectSingleNode.SelectSingleNode("AppId").InnerText;
            // this.IsSubscribe = selectSingleNode.SelectSingleNode("IsSubscribe").InnerText;
            // this.TimeStamp = selectSingleNode.SelectSingleNode("TimeStamp").InnerText;
            // this.NonceStr = selectSingleNode.SelectSingleNode("NonceStr").InnerText;
            // this.AppSignature = selectSingleNode.SelectSingleNode("AppSignature").InnerText;

            // }
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the app id.
        /// </summary>
        public string AppId { get; private set; }

        /// <summary>
        /// Gets the app signature.
        /// </summary>
        public string AppSignature { get; private set; }

        /// <summary>
        /// Gets the is subscribe.
        /// </summary>
        public string IsSubscribe { get; private set; }

        /// <summary>
        /// Gets the nonce str.
        /// </summary>
        public string NonceStr { get; private set; }

        /// <summary>
        /// Gets the open id.
        /// </summary>
        public string OpenId { get; private set; }

        /// <summary>
        ///     商户订单号
        /// </summary>
        public string Out_trade_no { get; set; }

        /// <summary>
        /// Gets the time stamp.
        /// </summary>
        public string TimeStamp { get; private set; }

        /// <summary>
        ///     交易号
        /// </summary>
        public string Transid { get; set; }

        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        public string Type { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Linux时间戳
        /// </summary>
        /// <param name="time">
        /// </param>
        /// <returns>
        /// 时间戳
        /// </returns>
        private int ConvertDateTimeInt(DateTime time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// 发货参数json
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string GetJson()
        {
            string timeInt = this.ConvertDateTimeInt(DateTime.Now).ToString(CultureInfo.InvariantCulture);

            var dictionary = new Dictionary<string, string>();
            dictionary.Add("appid", string.Empty);
            dictionary.Add("openid", this.OpenId);
            dictionary.Add("transid", this.Transid);
            dictionary.Add("out_trade_no", this.Out_trade_no);
            dictionary.Add("deliver_timestamp", timeInt);

            dictionary.Add("deliver_status", "1");
            dictionary.Add("deliver_msg", "ok");

            dictionary.Add("app_signature", this.GetappSignature(timeInt));

            dictionary.Add("sign_method", "sha1");

            return JsonConvert.SerializeObject(dictionary);
        }

        /// <summary>
        /// 签名
        /// </summary>
        /// <param name="convertDateTimeInt">
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string GetappSignature(string convertDateTimeInt)
        {
            var dictionary = new Dictionary<string, string>();
            dictionary.Add("appid", string.Empty);
            dictionary.Add("appkey", string.Empty);
            dictionary.Add("openid", this.OpenId);
            dictionary.Add("transid", this.Transid);
            dictionary.Add("out_trade_no", this.Out_trade_no);
            dictionary.Add("deliver_timestamp", convertDateTimeInt);

            dictionary.Add("deliver_status", "1");
            dictionary.Add("deliver_msg", "ok");

            return string.Empty;
        }

        #endregion
    }
}